package sg.edu.nus.comp.icreate.ivletalk.module;

import java.util.ArrayList;
import java.util.List;

import sg.edu.nus.comp.icreate.ivletalk.LoginActivity;
import sg.edu.nus.comp.icreate.ivletalk.ModuleNoRefreshBaseActivity;
import sg.edu.nus.comp.icreate.ivletalk.R;
import sg.edu.nus.comp.icreate.ivletalk.User;
import sg.edu.nus.comp.icreate.ivletalk.utils.net.DataPuller;
import sg.edu.nus.comp.icreate.ivletalk.voice.VoiceCommandManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends ModuleNoRefreshBaseActivity {
	public static final String APIKEY = "TxoHtrvdelbMqo8ccjb0V";
	public static final String LOGIN_INTENT = "getback";
	public static final String EXTRA_NAME_MODULE_POSITION = "sg.edu.nus.comp.icreate.ivletalk.extra.modulePosition";
	public static final List<Module> modules = new ArrayList<Module>();
	private static final String INVALID_ID = "00000000-0000-0000-0000-000000000000";
	private ListView moduleList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		moduleList = (ListView) findViewById(R.id.moduleList);
		ArrayAdapter<Module> adapter = new ArrayAdapter<Module>(this, android.R.layout.simple_list_item_1, modules);
		moduleList.setAdapter(adapter);
		moduleList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				((MainActivity) parent.getContext()).goToItem(position);
			}
		});
		try {
			if (getIntent().getBooleanExtra(EXTRA_NEED_REFRESH, false))
				getModules();
		} catch (Exception e) {
			Log.e("error", "Error in getExtra", e);
			finish();
		}

		checkVoiceCommand();
	}

	private void getModules() throws Exception {
		if (!User.isValid()) {
			Intent intent = new Intent(LOGIN_INTENT);
			intent.setClass(this, LoginActivity.class);
			startActivity(intent);
		}
		String url = "https://ivle.nus.edu.sg/api/Lapi.svc/Modules?APIKey=" + APIKEY + "&AuthToken=" + User.getInstance().getToken()
				+ "&Duration=0&IncludeAllInfo=false&output=xml";
		DataPuller moduleListPuller = new DataPuller(new ModuleListXmlParser());
		moduleListPuller.execute(url);
		@SuppressWarnings("unchecked")
		List<Module> list = (List<Module>) moduleListPuller.get();
		modules.clear();
		modules.addAll(list);
	}

	@Override
	public void goToItem(int position) {
		if (!INVALID_ID.equals(modules.get(position).getId())) {
			Intent intent = new Intent(this, ListModuleElementActivity.class);
			intent.putExtra(EXTRA_NAME_MODULE_POSITION, position);
			startActivity(intent);
		}
	}

	public void checkVoiceCommand() {
		String cmd = getIntent().getStringExtra(VoiceCommandManager.EXTRA_VOICE_COMMAND);
		if (cmd != null) {
			Intent intent = new Intent(this, ListModuleElementActivity.class);
			intent.putExtra(VoiceCommandManager.EXTRA_VOICE_COMMAND, cmd);
			intent.putExtra(EXTRA_NAME_MODULE_POSITION, getIntent().getIntExtra(VoiceCommandManager.EXTRA_POSITION, 0));
			startActivity(intent);
		}
	}

	@Override
	public List<Module> getDisplayedItemList() {
		return modules;
	}
}
